package com.kkb.controller;

import com.github.pagehelper.PageInfo;
import com.kkb.pojo.Menu;
import com.kkb.pojo.ResultVO;
import com.kkb.pojo.Role;
import com.kkb.pojo.User;
import com.kkb.service.RoleService;
import com.kkb.service.UserService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@Controller
@RequestMapping("user")
@ResponseBody
public class UserController {
    @Resource
    private UserService userService;
    @Resource
    private RoleService roleService;
    @RequestMapping(value = "list", method = RequestMethod.GET)
    public ResultVO<User> queryByPage(Integer pageNum, Integer pageSize, String userName) {
        if (pageNum == null || pageNum <= 0) {
            pageNum = 1;
        }
        if (pageSize == null || pageSize <= 0) {
            pageSize = 5;
        }
        PageInfo<User> info = userService.queryByPage(pageNum, pageSize, userName);
        return new ResultVO<>(info);
    }

    /**
     * 主键查询
     * @param userId
     * @return
     */
    @RequestMapping(value = "{id}", method = RequestMethod.GET)
    public ResultVO<User> queryById(@PathVariable("id") Integer userId) {
        User user = userService.queryById(userId);
        return new ResultVO<User>(user);
    }
    /**
     * 重复性验证
     * @param userLoginName
     * @return
     */
    @RequestMapping(value = "", method = RequestMethod.GET)
    public ResultVO<User> queryByUserLoginName(String userLoginName) {
        List<User> userList = userService.queryByUserLoginName(userLoginName);
        if(userList.size()>0){
            return new ResultVO<>(500);
        }else {
            return new ResultVO<>(200);
        }
    }

    /**
     * 获取角色列表
     * @return
     */
    @RequestMapping(value = "roleList", method = RequestMethod.GET)
    public ResultVO<Role> queryRoleList() {
        List<Role> roleList = roleService.queryRoleList();
        return new ResultVO<>(roleList);
    }

    /**
     * 添加用户
     * @param user
     * @return
     */
    @RequestMapping(value = "", method = RequestMethod.POST)
    public ResultVO<User> addUser(User user) {
        System.out.println(user);
        int i = userService.addUser(user);
        if (i == 1) {
            return new ResultVO<>(200);
        } else {
            return new ResultVO<>(500, "添加失败");
        }
    }
    @RequestMapping(value = "", method = RequestMethod.PUT)
    public ResultVO<User> updateUser(User user) {
        int i = userService.updateUser(user);
        ResultVO<User> resultVO = new ResultVO<>();
        if (i == 1) {
            resultVO.setCode(200);
        } else if (i == -1) {
            resultVO.setCode(500);
            resultVO.setMsg("用户名重复");
        } else {
            resultVO.setCode(500);
            resultVO.setMsg("更新失败");
        }
        return resultVO;
    }
    @RequestMapping(value = "{id}", method = RequestMethod.DELETE)
    public ResultVO<User> deleteUser(@PathVariable("id") Integer useId) {
        int i = userService.deleteUser(useId);
        if (i == 1) {
            return new ResultVO<>(200);
        } else {
            return new ResultVO<>(500, "删除失败");
        }
    }

    @RequestMapping(value = "", method = RequestMethod.DELETE)
    public ResultVO<User> deleteUsers(@RequestParam("ids[]") List<Integer> ids) {
        for (Integer id : ids) {
            System.out.println("userId:"+id);
        }
        int i = userService.deleteUsers(ids);
        if (i == ids.size()) {
            return new ResultVO<>(200);
        } else {
            return new ResultVO<>(500, "删除失败");
        }
    }
}
